function resetAndFlip(motor)
  speed = speedReading(motor)
  resetRotationCount(motor)
  forward(motor, -speed)
end

repeat
  light = activeLightReading(3)
  if light > 37 then
    forward(A)
    forward(B)
  else 
    backward(B)
    distance = 10
    repeat
      resetAndFlip(A)
      resetAndFlip(B)
      repeat until activeLightReading(3) < 37 or abs(rotationCounts(A)) > distance  
      distance = distance * 2
    until activeLightReading(3) < 37
  end
until isPressed(ESCAPE)


